package edu.beyond.employment.talent.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.beyond.employment.commom.Constant;
import edu.beyond.employment.commom.PageModel;
import edu.beyond.employment.commom.util.AppUtil;
import edu.beyond.employment.company.dao.ApplicationDao;
import edu.beyond.employment.company.entity.ApplicationEntity;
import edu.beyond.employment.talent.dao.ResumeDao;
import edu.beyond.employment.talent.entity.ResumeEntity;

/**
 * <pre>
 * 控制层（MVC的C层）：
 * 1、接收前台传来的参数
 * 2、调用业务逻辑
 * 3、控制转向
 * </pre>
 *
 * @author beyond.
 * @since 2016年1月11日
 */
public class ResumeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private ResumeDao resumeDao = new ResumeDao();
	private ApplicationDao applicationDao = new ApplicationDao();

	/**
	 * 表单的method="get"或地址栏提交会调用到此方法
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 表单的method="post"提交会调用到此方法
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 避免中文乱码
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("add".equals(command)) {
			add(request, response);
		} else if ("list".equals(command)) {
			list(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("preupdate".equals(command)) {
			preupdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 简历的新增
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int talentId = AppUtil.getTalentId(request, response);
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");

		// 2、调用业务逻辑
		ResumeEntity resumeEntity = new ResumeEntity();
		resumeEntity.setTalentId(talentId);
		resumeEntity.setIntention(intention);
		resumeEntity.setWorkExperience(workExperience);
		resumeEntity.setProjectExperience(projectExperience);
		int row = resumeDao.add(resumeEntity);

		// 3、转向
		if (row == 1) {
			request.setAttribute("success", "简历新增成功！");

		} else {
			request.setAttribute("fail", "简历新增失败！");
		}
		// 回到首页是显示简历信息

		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);
		List<ApplicationEntity> applicationList = applicationDao.list(talentId);
		request.setAttribute("applicationList", applicationList);
		// request.getRequestDispatcher("static/talent/talent-home.jsp").forward(request,
		// response);
		response.sendRedirect("ResumeServlet?command=main&row=" + row);
	}

	/**
	 * 避免简历重复新增
	 */

	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int row = Integer.parseInt(request.getParameter("row"));
		int talentId = AppUtil.getTalentId(request, response);
		// 2、调用业务逻辑

		// 3、转向
		if (row == 1) {
			request.setAttribute("success", "简历新增成功！");

		} else {
			request.setAttribute("fail", "简历新增失败！");
		}
		// 回到首页是显示简历信息

		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);
		List<ApplicationEntity> applicationList = applicationDao.list(talentId);
		request.setAttribute("applicationList", applicationList);
		request.getRequestDispatcher("view/talent/talent-home.jsp").forward(request, response);
	}

	/**
	 * 简历查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		// 2、调用业务逻辑
		int talentId = AppUtil.getTalentId(request, response);
		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		// 3、转向
		request.setAttribute("resumeList", resumeList);
		request.getRequestDispatcher("void/talent/talent-home.jsp").forward(request, response);
	}

	/**
	 * 简历的删除
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		int row = resumeDao.delete(id);
		// 3、转向
		if (row == 1) {
			request.setAttribute("success", "简历删除成功！");

		} else {
			request.setAttribute("fail", "简历删除失败！");
		}
		// 回到首页是显示简历信息
		int talentId = AppUtil.getTalentId(request, response);
		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);
		List<ApplicationEntity> applicationList = applicationDao.list(talentId);
		request.setAttribute("applicationList", applicationList);
		request.getRequestDispatcher("view/talent/talent-home.jsp").forward(request, response);
	}

	/**
	 * 简历修改前的查询
	 */
	private void preupdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		ResumeEntity resume = resumeDao.get(id);
		// 3、转向
		if (resume == null) {
			request.setAttribute("fail", "没有该简历信息！");
			// 回到首页是显示简历信息
			int talentId = AppUtil.getTalentId(request, response);
			List<ResumeEntity> resumeList = resumeDao.list(talentId);
			request.setAttribute("resumeList", resumeList);

			request.getRequestDispatcher("static/talent/talent-home.jsp").forward(request, response);
		} else {
			request.setAttribute("resume", resume);
			request.getRequestDispatcher("view/talent/resume-update.jsp").forward(request, response);
		}
	}

	/**
	 * 简历的真正修改
	 */

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		String talentName = request.getParameter("talentName");
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");
		// 2、调用业务逻辑
		ResumeEntity resumeEntity = new ResumeEntity();
		resumeEntity.setId(id);
		resumeEntity.setIntention(intention);
		resumeEntity.setWorkExperience(workExperience);
		resumeEntity.setProjectExperience(projectExperience);
		int row = resumeDao.update(resumeEntity);

		// 3、转向
		// 3、转向
		if (row == 1) {
			request.setAttribute("success", "简历修改成功！");

		} else {
			request.setAttribute("fail", "简历修改失败！");
		}
		// 回到首页是显示简历信息
		int talentId = AppUtil.getTalentId(request, response);
		List<ResumeEntity> resumeList = resumeDao.list(talentId);
		request.setAttribute("resumeList", resumeList);
		// List<ApplicationEntity> applicationList =
		// applicationDao.list(talentId);
		// request.setAttribute("applicationList", applicationList);
		int pageNo = Constant.DEFAULT_PAGE_NO;
		// 非登录操作
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		// 申请信息
		PageModel<ApplicationEntity> pageModel = applicationDao.listTalentApplyByPage(talentId, pageNo,
				Constant.DEFAULT_PAGE_SIZE);
		request.setAttribute("pageModel", pageModel);
		request.getRequestDispatcher("view/talent/talent-home.jsp").forward(request, response);
	}
}
